package agent;

import java.util.ArrayList;
import java.util.Map;
import java.util.Map.Entry;

import core.AgentInfo;

import exception.InvalidAgentClassException;

public abstract class AgentLoader {

	public static ArrayList<AgentInfo> loadAgents(Map<String, String> agentClasses) throws InvalidAgentClassException{
		ArrayList<AgentInfo> result = new ArrayList<AgentInfo>();
		for (Entry<String, String> entry : agentClasses.entrySet()) {
				String agentClass = entry.getValue();
				try {
					DominoAgent agent = (DominoAgent) Class.forName(agentClass).newInstance();
					String agentName = entry.getKey();
					result.add(new AgentInfo(agentName, agent));
				} catch (ClassNotFoundException e) {
					throw new InvalidAgentClassException("Agent class: \"" + agentClass + "\" not found.", e);
				} catch (ClassCastException e){ 
					throw new InvalidAgentClassException("Agent class: \"" + agentClass + "\" does not implement DominoAgent interface.", e);
				} catch (Exception e){
					throw new InvalidAgentClassException("Could not create agent class: \"" + agentClass + "\".", e);
				}
				

		}
		return result;
	}
	
}
